/***************************************************************************
 *   source.c                                                              *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "header.h"

/**
 * Function called at the engine stop, just before shot down. In here one
 * can put the finalization code e.g. releasing resources.
 */
void ENGINE_Exit(void) {

    /* TODO: place finalizing code here */

    MGPI_ENGINE_STOP
    MGPI_Exit();
    return;
}

/**
 * A initialization function, called at the begining of the application. In here
 * one can put any code concerning loading objects, preparing the scene etc.
 */
void ENGINE_Init(Roeskva_t *ctx) {
    {
        MGPI_ENGINE_CREATE_CAMERA(MGPI_PERSPECTIVE);

		ctx->setProperty(MGPI_CAMERA_VIEWPORT, MGPI_ENGINE_CAMERA, SCREEN_WIDTH, SCREEN_HEIGHT);

        /* TODO: place scene and engine setup here */

	} MGPI_ENGINE_START {
		//MGPI_InitContainer(NULL);
		//MGPI_GetRootComponent()->onEvent[MGPI_KEY_DOWN] = keyProc;
		//MGPI_GetRootComponent()->onEvent[MGPI_MOUSE_MOTION] = mouseMotionProc;
		//MGPI_GetRootComponent()->onEvent[MGPI_MOUSE_BUTTON_DOWN] = mouseButtonProc;
		//MGPI_GetRootComponent()->flags[0] = MGPI_ENGINE_CAMERA;
		//MGPI_GetRootComponent()->flags[1] = (int)ctx;
		////////////////////////////////////////////////////
		ct.component.onEvent[MGPI_KEY_DOWN] = keyProc;
		ct.component.onEvent[MGPI_MOUSE_MOTION] = mouseMotionProc;
		ct.component.onEvent[MGPI_MOUSE_BUTTON_DOWN] = mouseButtonProc;
		ct.component.flags[0] = MGPI_ENGINE_CAMERA;
		ct.component.flags[1] = (int)ctx;
		////////////////////////////////////////////////////

		//MGPI_InitEvents(MGPI_THREAD_EVENTS, NULL);
	}
    return;
}

/**
 * The engine main rendering loop function. Place here any code refering
 * to rendering process. Note that calling here any logic processing functions
 * is not advisable.
 */
void ENGINE_Render(Roeskva_t *ctx) {
	float4 vect = MGPI_DEFAULT_VECTOR;

    ctx->updateCamera(MGPI_ENGINE_CAMERA);
    {

        /* TODO: place your rendering code here */

		///////////////////////////////////////////////
		glPushMatrix();
		{
			glTranslatef(0, -0.5f, -1);
			glBegin(GL_TRIANGLES);
			{
				glColor3f(1,0,0);
				glVertex3f(0.5f, 0.0f, 0.0f);
				glColor3f(0,1,0);
				glVertex3f(0.0f, 1.0f, 0.0f);
				glColor3f(0,0,1);
				glVertex3f(-0.5f, 0.0f, 0.0f);
			}
			glEnd();
		}
		glPopMatrix();

		g3dDrawGrid(50.0f, 1.0f, -2.0f, 1, 1, 1);

		glPushMatrix();
		{
			glTranslatef(0, 0, -3);

			g3dGetCamVect(&vect, MGPI_ENGINE_CAMERA, MGPI_CAMERA_FORWARD);
			vect[X] = vect[X] * 0.5f + 0.5f;
			vect[Y] = vect[Y] * 0.5f + 0.5f;
			vect[Z] = vect[Z] * 0.5f + 0.5f;
			glColor3fv(vect);
			g3dDrawTeapot(1.0f, 0, 0);
		}
		glPopMatrix();
		////////////////////////////////////////////////

		ctx->renderDiagnostic();
		SDL_Delay(12);
    }
    ctx->commitChanges();
    return;
}
